package com.john.booter.module.news.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.john.booter.domain.entity.News;
import com.john.booter.module.news.dao.NewsDao;
import com.john.booter.pagination.DataTable;
import com.john.booter.pagination.Pager;

@Repository
public class NewsDaoImpl implements NewsDao {
	
	@PersistenceContext
	private EntityManager em;
	
	@Override
	public void save(News news) {
		em.persist(news);
	}

	@Override
	public void delete(String id) {
		Query query = em.createQuery("delete from News t where t.id = ?1");
		query.setParameter(1, id);
		query.executeUpdate();
	}

	@Override
	public void update(News news) {
		em.merge(news);
	}

	@Override
	public List<News> findAll() {
		return em.createQuery("from News t", News.class).getResultList();
	}

	@Override
	public News findById(String id) {
		TypedQuery<News> query = em.createQuery("from News t where t.id = ?1", News.class); 
		query.setParameter(1, id);
		List<News> list = query.getResultList();
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public DataTable<News> findDataTable(Pager pg, String creator) {
		//该数组顺序按table列顺序排列
		String orderby = (new String[] { "title", "createTime", "updateTime", "creator", "state", "id" })[pg
				.getOrderColumn()];
		String searchValue = pg.getSearchValue();
		Integer firstResult = pg.getStart();
		Integer pageSize = pg.getPageSize();

		// 组装查询语句
		StringBuilder querySql = new StringBuilder("select t from News t");
		StringBuilder countSql = new StringBuilder(
				"select count(t.id) from News t");
		StringBuilder whereSql = new StringBuilder(" where 1=1");
		String orderBy = " order by t." + orderby + " " + pg.getOrderDir();

		// 查询条件
		if (StringUtils.isNotEmpty(searchValue)) {
			whereSql.append(" and t.title like :title");
		}
		if (StringUtils.isNotEmpty(creator)) {
			whereSql.append(" and t.creator like :creator");
		}
		querySql.append(whereSql).append(orderBy);

		// 数据查询
		TypedQuery<News> query = em.createQuery(querySql.toString(), News.class);
		setParameter(query, searchValue, creator);
		
		List<News> data = query.setFirstResult(firstResult).setMaxResults(pageSize).getResultList();
		// 总记录统计
		Query queryTotal = em.createQuery(countSql.toString());
		int recordsTotal = ((Long) queryTotal.getSingleResult()).intValue();

		// 条件查询记录统计
		countSql.append(whereSql);
		Query queryFilter = em.createQuery(countSql.toString());
		setParameter(queryFilter, searchValue, creator);
		int recordsFiltered = ((Long) queryFilter.getSingleResult()).intValue();

		// 分页封装
		DataTable<News> dataTable = new DataTable<News>();
		dataTable.setRecordsTotal(recordsTotal);
		dataTable.setRecordsFiltered(recordsFiltered);
		dataTable.setData(data);
		dataTable.setDraw(pg.getDraw());
		return dataTable;
	}
	
	/**拼接查询参数*/
	private void setParameter(Query query, String cnd, String creator) {
		if (StringUtils.isNotEmpty(cnd)) {
			query.setParameter("title", "%" + cnd + "%");
		}
		if(StringUtils.isNotEmpty(creator)){
			query.setParameter("creator", creator + "%");
		}
	}

}
